<?php


namespace App\Model\Logic;

use App\Exception\HttpException;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Swoft\Bean\Annotation\Mapping\Bean;
use App\Model\Dao\ManagerDao;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Session\HttpSession;

/**
 * Class ManagerLogic
 * @Bean(name="ManagerLogic")
 * @package App\Model\Logic
 */
class ManagerLogic
{
    /**
     * @Inject()
     * @var ManagerDao
     */
    private $managerDao;

    public function updatePassword($data){
        //数据验证
        validate($data, 'LoginValidator', ['oldpassword', 'password', 'repassword']);
        //判断旧密码是否正确
        $username = HttpSession::current()->get('username');
        $user = $this->managerDao->getUserFromUsername($username);
        if ( ! password_verify($data['oldpassword'], $user['password'])){
            throw new HttpException('旧密码输入不正确');
        }
        //密码的更新
        if ($this->managerDao->updatePassword($data['password'])){
            throw new HttpException('密码修改成功', 0);
        }
        throw new HttpException('密码修改失败', 1);
    }
}
